#include "CollaborationProtocol.h"
#include <wx/tokenzr.h>
#include <wx/utils.h>  // 添加这个头文件以包含 wxGetLocalTime

CollaborationMessage::CollaborationMessage()
    : m_type(MODEL_EDIT), m_timestamp(wxDateTime::Now())
{
    m_messageId = GenerateMessageId();
}

CollaborationMessage::CollaborationMessage(MessageType type, const wxString& sender, const wxString& content)
    : m_type(type), m_sender(sender), m_content(content), m_timestamp(wxDateTime::Now())
{
    m_messageId = GenerateMessageId();
}

wxString CollaborationMessage::Serialize() const
{
    wxString data;
    data << m_type << "|" << m_sender << "|" << m_content << "|" 
         << m_timestamp.FormatISOCombined() << "|" << m_messageId;
    return data;
}

bool CollaborationMessage::Deserialize(const wxString& data)
{
    wxArrayString tokens = wxStringTokenize(data, "|");
    if (tokens.size() < 5) return false;

    long type;
    if (!tokens[0].ToLong(&type)) return false;
    m_type = static_cast<MessageType>(type);
    
    m_sender = tokens[1];
    m_content = tokens[2];
    m_timestamp.ParseISOCombined(tokens[3]);
    m_messageId = tokens[4];
    
    return true;
}

wxString CollaborationMessage::GenerateMessageId() const
{
    // 使用更简单的方法生成消息ID，避免依赖问题
    static int counter = 0;
    counter++;
    return wxString::Format("msg_%d_%s", 
        counter,
        wxDateTime::Now().Format("%H%M%S").c_str());
}
